<?php
/**
 * @file column.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015年7月30日 下午2:49:50 $
 * @brief app 栏目管理 $
 */
namespace haibao\app\business;

use haibao\app\data\ColumnArea as ColumnAreaData;
use haibao\app\data\ColumnCategory as ColumnCategoryData;
use haibao\app\data\Region as RegionData;
use haibao\app\data\Area as AreaData;
use haibao\app\data\ColumnArea as ColumnArea;
use haibao\app\model\data\ColumnCategory;

class Column extends \haibao\app\business\Base {
	
	private static $column = 'column';
	private static $area = 'area';
	
	//API接口
	public static function getTopColumnList(){
		$data = new ColumnCategoryData();
		return $data->getTopColumnList();
	}
	
	public static function getColumnAreaList(){
		$data = new ColumnArea();
		return $data->getColunmArea();
	}
	
	public static function getRegionList(){
		$data = new RegionData();
		return $data->getRegion();
	}
	//CMS后台
	//栏目
	/**
	 * 获取栏目列表
	 * @param \haibao\app\model\filter\ColumnCategory $columnCategoryFilter
	 */
	public static function getColumnList($columnCategoryFilter){
		$columnList = new ColumnCategoryData();
		return $columnList->getAll($columnCategoryFilter);
	}
	
	/**
	 * 获取所有顶级栏目
	 */
	public static function getTopColumnListByName($name){
		$columnList = array();
		$columnData =  new ColumnCategoryData();
		$dataList = $columnData->getColumn();
		foreach($dataList as $list){
			if(!$list['ParentId']){
				if(empty($name) || strpos($list['Name'], $name) !== false){
					$columnList[] = $list;
				}
			}
		}
		return $columnList;
	}
	
    /**
     *获取所有子类栏目
     */
	public static function getChildColumnListByName($name){
		$columnList = array();
		$columnData =  new ColumnCategoryData();
		$dataList = $columnData->getColumn();
		foreach($dataList as $list){
			if($list['ParentId']){
				if(empty($name) || strpos($list['Name'], $name) !== false){
					$columnList[] = $list;
				}
			}
		}
		return $columnList;
	}
	
	public static function getChildColumnIds($parentId){
		$columnIds = array();
		$columnData =  new ColumnCategoryData();
		$dataList = $columnData->getColumn();
		foreach($dataList as $list){
			if($list['ParentId'] == $parentId){
				$columnIds[] = $list['Id'];
			}
		}
		return $columnIds;
	}
	
	/**
	 * 新增栏目
	 */
	public static function addColumn($model){
		self::checkOperation(self::FUNC_CODE_APP_ARTICLE_COLUMN);
		self::checkColumnModel($model);
		$columnCategoryData = new ColumnCategoryData();
		$columnCategoryData->addColumn($model);
		self::saveOperateHistory(self::FUNC_CODE_APP_ARTICLE_COLUMN, $model->toArray(true,true));
	}
	
	/**
	 * 修改栏目状态
	 */
	public static function updateColumnStatus($id, $status){
		self::checkOperation(self::FUNC_CODE_APP_ARTICLE_COLUMN);
		$columnData = new ColumnCategoryData();
		$columnData->updateColumnStatus($id, $status);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = \haibao\app\model\data\ColumnCategory::STATUS_DELETE;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_APP_ARTICLE_COLUMN,$arrSet);
	}
	
	/**
	 * 修改栏目功能状态
	 */
	public static function changeStatus($status = \haibao\app\model\data\ColumnCategory::COLUMN_STATUS_YES){
		$columnData = new ColumnCategoryData();
		if($status != \haibao\app\model\data\ColumnCategory::COLUMN_STATUS_YES){
			$status = \haibao\app\model\data\ColumnCategory::COLUMN_STATUS_NO;
		}
		$columnData->changeStatus($status);
	}
	
	public static function getIsShowColumn(){
		$columnData = new ColumnCategoryData();
		return $columnData->getIsShowColumn();
	}
	
	/**
	 * 修改栏目排序
	 */
	public static function updateColumnSort($columnId, $arrSet){
		self::checkOperation(self::FUNC_CODE_APP_ARTICLE_COLUMN);
		$columnData = new ColumnCategoryData();
		$columnData->updateColumnSort($columnId, $arrSet);
		self::saveOperateHistory(self::FUNC_CODE_APP_ARTICLE_COLUMN, $arrSet);
	}
	
	private static function checkColumnModel($model){
		if(!$model->Name){
			throw new BusinessException('栏目名不能为空');
		}
		$data = new ColumnCategoryData();
		if($model->ParentId){
			$result = $data->getColumnByParentId($model->ParentId);
			if(!empty($result)){
				foreach ($result as $k=>$v){
					if($model->Name == $v['Name']){
						throw new BusinessException('栏目名重复');
					}
				}
			}
		}else {
			$result = $data->getTopColumnList();
			foreach ($result as $k=>$v){
				if($model->Name == $v['Name']){
					throw new BusinessException('顶级栏目名重复');
				}
			}
		}
		
	}
	
	//地区
	/**
	 * 获取地标列表
	 * @param \haibao\app\model\filter\ColumnArea $columnAreaFilter
	 */
	public static function getAreaList($columnAreaFilter){
		$columnArea = new AreaData();
		return $columnArea->getAll($columnAreaFilter);
	}
    /**
     * 得到所有地区
     */
    public static function getAreaListByName($name){
        $data = new AreaData();
        $dataList = $data->getAllArea();
        $areaList = array();
        foreach($dataList as $list){
        	if(empty($name) || strpos($list['Name'], $name) !== false){
        		$areaList[] = $list;
        	}
        }
        return $areaList;
    }
	
	/**
	 * 新增地标
	 */
	public static function addArea($model){
		self::checkOperation(self::FUNC_CODE_APP_AREA);
		self::checkColumnAreaModel($model);
		$columnAreaData = new ColumnAreaData();
		$columnAreaData->addArea($model);
		self::saveOperateHistory(self::FUNC_CODE_APP_AREA,$model->toArray(true,true));
	}
	
	/**
	 * 修改地标状态
	 */
	public static function updateColumnAreaStatus($id){
		self::checkOperation(self::FUNC_CODE_APP_AREA);
		$columnAreaData = new ColumnAreaData();
		$columnAreaData->updateColumnAreaStatus($id);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = \haibao\app\model\data\ColumnArea::STATUS_DELETE;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_APP_AREA,$arrSet);
	}
	
	/**
	 * 修改地标排序
	 */
	public static function updateAreaSort($areaId, $arrSet){
		self::checkOperation(self::FUNC_CODE_APP_AREA);
		$areaData = new ColumnAreaData();
		$areaData->updateAreaSort($areaId, $arrSet);
		self::saveOperateHistory(self::FUNC_CODE_APP_AREA, $arrSet);
	}
	
	/**
	 * 获取栏目,地标,地域最大sort
	 */
	public static function getSortMax($source){
		if($source == self::$column){
			$columnData =  new ColumnCategoryData();
			return $columnData->getColumnMaxSort();
		}
		if($source == self::$area){
			$areaData =  new ColumnAreaData();
			return $areaData->getAreaMaxSort();
		}
		if($source == 'region'){
			$regionData =  new \haibao\app\data\Region();;
			return $regionData->getRegionMaxSort();
		}
	}
	
	public static function getColumnByName($name){
		$data =  new ColumnCategoryData();
		return $data->getColumnByName($name);
	}
	
	/**
	 * 验证新增地标
	 */
	private static function checkColumnAreaModel($model){
		if(!$model->AreaId){
			throw new BusinessException('地标ID不能为空');
		}
		$columnAreaFileter = new ColumnAreaData();
		$result = $columnAreaFileter->getColunmArea();
		foreach($result as $key=>$v){
			if($model->AreaId == $v['Id']){
				throw new BusinessException('地标已在列表中');
			}
		}
	}
	
	/**
	 * 根据Id获得一条地标数据
	 */
	public static function getAreaById($area_id){
		$columnAreaFileter = new ColumnAreaData();
		return $columnAreaFileter->getAreaById($area_id);
	}
	
	//地域
	/**
	 * 新增地域
	 */
	public static function addRegion($model){
		self::checkOperation(self::FUNC_CODE_APP_REGION);
		self::checkRegionModel($model);
		$regionData = new RegionData();
		$regionData->addRegion($model);
		self::saveOperateHistory(self::FUNC_CODE_APP_REGION,$model->toArray(true,true));
	}
	
	
	/**
	 * 修改地域排序
	 */
	public static function updateRegionSort($regionId, $arrSet){
		self::checkOperation(self::FUNC_CODE_APP_REGION);
		$regionData = new RegionData();
		$regionData->updateRegionSort($regionId, $arrSet);
		self::saveOperateHistory(self::FUNC_CODE_APP_REGION, $arrSet);
	}
	
	/**
	 * 修改地域状态
	 */
	public static function updateRegionStatus($id, $status){
		self::checkOperation(self::FUNC_CODE_APP_REGION);
		$regionData = new RegionData();
		$regionData->updateRegionStatus($id, $status);
		$arrSet['Id'] = $id;
		$arrSet['Status'] = $status;
		$arrSet['CreateBy'] = self::getCurrentUser ()->Id;
		$arrSet['UpdateTime'] = new \DateTime();
		self::saveOperateHistory(self::FUNC_CODE_APP_REGION,$arrSet);
	}
	
	/**
	 * 获取地域列表
	 */
	public static function getAllArticleRegion($query){
		$regionData = new RegionData();
		return $regionData->getAll($query);
	}
	
	
	/**
	 * 根据地域名称获取数据
	 */
	public static function getRegionByName($regionName){
		$regionData = new RegionData();
		return $regionData->getRegionByName($regionName);
	}
	
	/**
	 * 获取所有地域数据(cache)
	 */
	public static function getArticleRegion(){
		$regionData = new RegionData();
		return $regionData->getArticleRegion();
	}
	
	private static function checkRegionModel($model){
		if(!$model->RegionName){
			throw new BusinessException('地域名不能为空');
		}
		$data = new RegionData();
		if($model->RegionName){
			$result = $data->getRegionByName($model->RegionName);
			if(!empty($result)){
				throw new BusinessException('地域名重复');
			}
		}
	}
}
